package com.oa.ylms.apps.business.news.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.news.INewsCategoryService;
import com.oa.ylms.apps.business.news.INewsInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.dao.news.INewsInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.news.NewsInfoValue;

public class NewsInfoServiceImpl implements INewsInfoService{
	private INewsInfoDao infoDao;
	private INewsCategoryService categoryService;
	private NewsInfoValue newsInfo;
	private ArchiveInfoValue archiveInfo;
	private IStaffInfoService staffInfoService;

	@Override
	public int addNewsInfo(int categoryId, String title,String content,StaffInfoValue staffInfo) throws Exception {
		newsInfo.setNewsCategory(categoryService.queryCategoryById(categoryId));
		newsInfo.setStaffInfo(staffInfo);
		newsInfo.setTitle(title);
		newsInfo.setContent(content);
		newsInfo.setPublishTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		return infoDao.addNewsInfo(newsInfo);
	}

	@Override
	public int countTPages(Integer categoryId,String publishTime, String title, String staffName, int rows) throws Exception {
		if(categoryId != null){
			newsInfo.setNewsCategory(categoryService.queryCategoryById(categoryId));
		}
		if(publishTime != null){
			newsInfo.setPublishTime(DateUtil.toDateWithoutHours(publishTime));
		}
		if(title != null){
			title = "%"+title+"%";
			newsInfo.setTitle(title);
		}
		if(staffName != null){
			staffName = "%"+staffName+"%";
			archiveInfo.setStaffName(staffName);
		}
		return infoDao.countTPages(newsInfo,archiveInfo, rows);
	}
	@Override
	public boolean deleteNewsInfo(String newsIds) throws Exception {
		String[] newsIdStr = newsIds.split(",");
		for(String newsId : newsIdStr){
			newsInfo=queryNewsById(Integer.parseInt(newsId));
			if(newsInfo!=null){
				infoDao.deleteNewsInfo(newsInfo);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifyNewInfo(StaffInfoValue staffInfo,int newsId, int categoryId, String title, String content) throws Exception {
		newsInfo = queryNewsById(newsId);
		if(newsInfo != null){
			newsInfo.setTitle(title);
			newsInfo.setContent(content);
			newsInfo.setNewsCategory(categoryService.queryCategoryById(categoryId));
			newsInfo.setStaffInfo(staffInfo);
			return infoDao.modifyNewsInfo(newsInfo);
		}
		return false;
	}

	@Override
	public List<NewsInfoValue> queryNewsWithPage(Integer categoryId,String publishTime,String title, String staffName, int rows, int pageNo)throws Exception {
		if(categoryId != null){
		newsInfo.setNewsCategory(categoryService.queryCategoryById(categoryId));
		}
		if(publishTime != null){
			newsInfo.setPublishTime(DateUtil.toDateWithoutHours(publishTime));
		}
		if(title != null){
			title = "%"+title+"%";
			newsInfo.setTitle(title);
		}
		if(staffName != null){
			staffName = "%"+staffName+"%";
			archiveInfo.setStaffName(staffName);
		}
		return infoDao.qureyNewsWithPage(newsInfo, archiveInfo, rows, pageNo);
	}

	@Override
	public NewsInfoValue queryNewsById(int newsId) throws Exception {
		newsInfo.setNewsId(newsId);
		return infoDao.qureyNewsById(newsInfo);
	}
	
	@Override
	public int getTotalNumber () throws Exception{
		return infoDao.getTotalNumbers();
	}

	public List<NewsInfoValue> queryTopNews(int topNum) throws Exception {
 
		return infoDao.qureyTopNews(topNum);
	}
	
	
	public NewsInfoValue getNewsInfo() {
		return newsInfo;
	}

	public void setNewsInfo(NewsInfoValue newsInfo) {
		this.newsInfo = newsInfo;
	}

	public void setInfoDao(INewsInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public INewsInfoDao getInfoDao() {
		return infoDao;
	}

	public void setCategoryService(INewsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public INewsCategoryService getCategoryService() {
		return categoryService;
	}
	
	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

}
